package company;

import java.util.Scanner;

/**
 * 给你一个由 '1'（陆地）和 '0'（水）组成的的二维网格，请你计算网格中岛屿的数量。
 * <p>
 * 岛屿总是被水包围，并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
 * <p>
 * 此外，你可以假设该网格的四条边均被水包围。
 * <p>
 *  
 * <p>
 * 示例 1：
 * <p>
 * 输入：grid = [
 * ["1","1","1","1","0"],
 * ["1","1","0","1","0"],
 * ["1","1","0","0","0"],
 * ["0","0","0","0","0"]
 * ]
 * 输出：1
 * 示例 2：
 * <p>
 * 输入：grid = [
 * ["1","1","0","0","0"],
 * ["1","1","0","0","0"],
 * ["0","0","1","0","0"],
 * ["0","0","0","1","1"]
 * ]
 * 输出：3
 */

/**
 * 思路：
 * 用深度搜索
 * 1.先找到1的节点，置为0，
 * 2.同时这个节点的左右上下都遍历，将1置为0
 */
public class IsLand {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String replace = s.replace("[[", "");
        String replace1 = replace.replace("]]", "");
        String[] split = replace1.split("\\],\\[");
        int row = split.length;
        String[] split1 = split[0].split(",");
        int col = split1.length;
        char[][] graph = new char[row][col];
        for (int i = 0; i < row; i++) {
            String[] split3 = split[i].split(",");
            for (int j = 0; j < col; j++) {
                graph[i][j] = split3[j].charAt(1);
            }
        }
        System.out.println(numIslands(graph));
    }

    public static int numIslands(char[][] graph) {
        int countIsland = 0;
        for (int i = 0; i < graph.length; i++) {
            for (int j = 0; j < graph[0].length; j++) {
                if (graph[i][j] == '1') {
                    graph[i][j] = '0';
                    countIsland++;
                    dfs(graph, i, j);
                }
            }
        }
        return countIsland;
    }

    private static void dfs(char[][] graph, int row, int col) {
        if (row - 1 >= 0 && graph[row - 1][col] == '1') {
            graph[row - 1][col] = '0';
            dfs(graph, row - 1, col);
        }
        if (row + 1 < graph.length && graph[row + 1][col] == '1') {
            graph[row + 1][col] = '0';
            dfs(graph, row + 1, col);
        }
        if (col - 1 >= 0 && graph[row][col - 1] == '1') {
            graph[row][col - 1] = '0';
            dfs(graph, row, col - 1);
        }
        if (col + 1 < graph[0].length && graph[row][col + 1] == '1') {
            graph[row][col + 1] = '0';
            dfs(graph, row, col + 1);
        }
    }
}
